//
//  XinfomationReviewSubViewController.m
//  Bees
//
//  Created by Xiaohan on 2018/12/21.
//  Copyright © 2018 xiaohan. All rights reserved.
//

#import "XinfomationReviewSubViewController.h"
#import "XInfomationTableCell.h"
#import "XReviewTableViewCell.h"
#import "XReviewTwoTableViewCell.h"
#import "FoundServer.h"
#import "WebInfoViewController.h"
#import "ShareViewController.h"
@interface XinfomationReviewSubViewController ()
@property (nonatomic,strong) FoundServer *server;

@end

@implementation XinfomationReviewSubViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self.tableView.mj_header beginRefreshing];
}
- (void)loadMore:(BOOL)isMore{
    if (self.type == InfomationReviewTypeInfomation) {
        [self.server getInformationListIsMore:isMore completion:^(BOOL success, BOOL isHaveNextPage, NSString * _Nonnull msg) {
            [self endHeaderFooterRefreshingIsHaveNextPage:isHaveNextPage];
            if (success) {
                [self.tableView reloadData];
            }else{
                [self.view makeToast:msg];
            }
        }];
    }else if (self.type == InfomationReviewTypeReview){
        [self.server getEvaluationListIsMore:isMore completion:^(BOOL success, BOOL isHaveNextPage, NSString * _Nonnull msg) {
            [self endHeaderFooterRefreshingIsHaveNextPage:isHaveNextPage];
            if (success) {
                [self.tableView reloadData];
            }else{
                [self.view makeToast:msg];
            }
        }];
    }else{
        [self.server getNoticeListIsMore:isMore completion:^(BOOL success, BOOL isHaveNextPage, NSString * _Nonnull msg) {
            [self endHeaderFooterRefreshingIsHaveNextPage:isHaveNextPage];
            if (success) {
                [self.tableView reloadData];
            }else{
                [self.view makeToast:msg];
            }
        }];
    }
}
- (void)setupUI{
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
#pragma mark - table

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.type==InfomationReviewTypeInfomation) {
        return self.server.informationList.count;
    }else if (self.type==InfomationReviewTypeReview){
        return self.server.evaluationList.count;
    }else{
        return self.server.noticeList.count;
    }
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.type==InfomationReviewTypeInfomation) {
        XInfomationTableCell *cell = [XInfomationTableCell cellForTableView:tableView];
        cell.indexPath = indexPath;
        cell.model = self.server.informationList[indexPath.row];
        kWeakSelf(self);
        cell.ShareBlock = ^(InformationModel * _Nonnull shareM) {
            ShareViewController *vc = [ShareViewController new];
            vc.shareM = shareM;
            vc.type = 1;
            [weakself.navigationController hh_pushBackViewController:vc];
        };
        cell.DidDownLoadBlock = ^(NSIndexPath * _Nonnull indexPath) {
            [weakself.tableView reloadRowAtIndexPath:indexPath withRowAnimation:UITableViewRowAnimationFade];
        };
        return cell;
    }else if(self.type==InfomationReviewTypeReview){
       EvaluationModel * model = self.server.evaluationList[indexPath.row];
        if (model.big_cover.length && model.is_top) {
            XReviewBigTableViewCell * cell = [XReviewBigTableViewCell cellForTableView:tableView];
            cell.model = model;
            return cell;
        }else{
            XReviewTwoTableViewCell * cell = [XReviewTwoTableViewCell cellForTableView:tableView];
            cell.model = model;
            return cell;
        }
    }else{
        XInfomationTableCell *cell = [XInfomationTableCell cellForTableView:tableView];
        cell.indexPath = indexPath;
        cell.model = self.server.noticeList[indexPath.row];
        kWeakSelf(self);
        cell.ShareBlock = ^(InformationModel * _Nonnull shareM) {
            ShareViewController *vc = [ShareViewController new];
            vc.shareM = shareM;
            vc.type = 3;
            [weakself.navigationController hh_pushBackViewController:vc];
        };
        cell.DidDownLoadBlock = ^(NSIndexPath * _Nonnull indexPath) {
            [weakself.tableView reloadRowAtIndexPath:indexPath withRowAnimation:UITableViewRowAnimationFade];
        };
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.type==InfomationReviewTypeReview) {
        EvaluationModel * model = self.server.evaluationList[indexPath.row];
        WebInfoViewController*vc = [WebInfoViewController new];
        vc.gotoURL = model.redirect_url;
        vc.title = model.title;
        vc.isShare = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.type == InfomationReviewTypeInfomation) {
        InformationModel*model = self.server.informationList[indexPath.row];
        return model.coverImgH+model.contentH;
    }else if (self.type == InfomationReviewTypeReview){
        EvaluationModel*model = self.server.evaluationList[indexPath.row];
        return model.contentH;
    }else{
        InformationModel*model = self.server.noticeList[indexPath.row];
        return model.coverImgH+model.contentH;
    }
}

#pragma mark - getter
- (FoundServer*)server{
    if (!_server) {
        _server = [FoundServer new];
    }
    return _server;
}

@end
